<?php
declare (strict_types=1);

namespace app\course\model\record;

use app\common\model\BaseModel;
use app\course\logic\base\CourseCert;

/**
 * 用户学习记录模型
 */
class LearnCourseFinished extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'user_learn_course_finished';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'          => 'integer',
        'create_time' => 'integer',
    ];

    /**
     * 获取已完成课程的学员
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-10-13
     *
     * @param  [type] $courseId [description]
     * @param  [type] $stu      [description]
     */
    public function getFinishedStu($courseId, $stu = null)
    {
        return $stu ?
            $this->whereCourseId($courseId)->whereIn('user_id', $stu)->column('user_id') :
            $this->whereCourseId($courseId)->column('user_id');
    }

// 模型事件

    /**
     * 成功创建之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2021-10-11
     */
    public static function onAfterInsert($data)
    {
        // 检查是否发放证书
        app(CourseCert::class)->addCert(intval($data->course_id), intval($data->user_id));
    }
}
